'use strict';
const Controller = require('egg').Controller;
const qs = require('qs');

class GoodsController extends Controller {
  // 查询sku是否唯一
  async checkSku() {
    const { ctx } = this;
    const res = await this.service.goods.checkSku(qs.parse(ctx.query));
    ctx.status === 200;
    ctx.body = {
      code: 0,
      data: res,
    };
  }
  // 保存商品
  async saveGoods() {
    const { ctx } = this;
    let errors;
    try {
      ctx.validate({
        info: { type: 'object', required: true, rule: {
          category_id: { type: 'int', required: true },
          list_pic_url: { type: 'string', required: true },
          gallery: { type: 'array', required: true, min: 1 },
          name: { type: 'string', required: true },
          goods_brief: { type: 'string', required: true },
        } },
        specData: { type: 'array', required: true, min: 1, rule: {
          cost: { type: 'string', required: true },
          goods_sn: { type: 'string', required: true },
          goods_weight: { type: 'string', required: true },
          retail_price: { type: 'string', required: true },
          value: { type: 'string', required: true },
        } },
        specValue: { required: true },
      }, ctx.request.body);

    } catch (error) {
      errors = error.errors;
      ctx.logger.error('error:', error.errors);
    }
    if (errors) {
      ctx.status = 200;
      ctx.body = {
        code: -1,
        msg: errors || '参数出错',
      };
      return;
    }

    // 校验通过后，进行具体逻辑处理
    const res = await this.service.goods.saveGoods(ctx.request.body);

    if (res.code === 0) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res.data,
      };
    } else if (res.code === 1) {
      ctx.status = 200;
      ctx.body = {
        code: 1,
        msg: '该商品已存在',
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: 0,
        msg: '服务器报错',
      };
    }


  }
  async getGoodsList() {
    // 获取商品列表
    const { ctx } = this;

    const res = await this.service.goods.getGoodsList(ctx.query);
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '服务器报错，请稍后重试',
      };
    }
  }
  // 更加id查询商品
  async getGoodsById() {
    // 获取商品列表
    const { ctx } = this;

    const res = await this.service.goods.getGoodsById(ctx.query.id);
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        data: res,
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '服务器报错，请稍后重试',
      };
    }
  }
  // 更新排序
  async updateGoods() {
    const { ctx } = this;
    const res = await this.service.goods.updateGoods(ctx.request.body);
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        msg: '更新成功',
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '服务器报错，请稍后重试',
      };
    }
  }
  // 更新产品
  async updateProduct() {
    const { ctx } = this;
    const res = await this.service.goods.updateProduct(ctx.request.body);
    if (res) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        msg: '更新成功',
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '服务器报错，请稍后重试',
      };
    }
  }
  async deleteGoodsById() {
    const { ctx } = this;
    const id = ctx.request.body.id;
    if (!id) {
      ctx.status = 400;
      ctx.body = {
        code: -1,
        msg: '商品id不存在',
      };
      return;
    }
    const res = await this.service.goods.deleteGoodsById(id);
    if (res.code === 0) {
      ctx.status = 200;
      ctx.body = {
        code: 0,
        msg: '更新成功',
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: -1,
        msg: '服务器报错，请稍后重试',
      };
    }
  }

}
module.exports = GoodsController;
